local hx__sxhx = fk.CreateSkill {
  name = "hx__sxhx",
}

local U = require "packages/utility/utility"

hx__sxhx:addEffect(fk.RoundStart, {
  anim_type = "control",
  priority = 2,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__sxhx.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_names = player:getMark("hx__sxhx_names")
    if all_names == 0 then
      all_names = Fk:getAllCardNames("btde", true)
      room:setPlayerMark(player, "hx__sxhx_names", all_names)
    end
    local names = table.simpleClone(all_names)
    if player:getMark("@[private]$hx__sxhx_names") ~= 0 then
      for _, name in ipairs(U.getPrivateMark(player, "$hx__sxhx_names")) do
        table.removeOne(names, name)
      end
    end
    local mark = U.askForChooseCardNames(room, player, names, 1, 1, hx__sxhx.name,
      "#hx__sxhx-choice", all_names, true)
    if #mark > 0 then
      room:addPlayerMark(player, "mou__kanpo_times", #mark)
      U.setPrivateMark(player, "$hx__sxhx_names", mark)
    end
  end
})


hx__sxhx:addEffect(fk.TurnStart, {
  anim_type = "control",
  priority = 2,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__sxhx.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_names = player:getMark("hx__sxhx_names")
    if all_names == 0 then
      all_names = Fk:getAllCardNames("btde", true)
      room:setPlayerMark(player, "hx__sxhx_names", all_names)
    end
    local names = table.simpleClone(all_names)
    if player:getMark("@[private]$hx__sxhx_names") ~= 0 then
      for _, name in ipairs(U.getPrivateMark(player, "$hx__sxhx_names")) do
        table.removeOne(names, name)
      end
    end
    local mark = U.askForChooseCardNames(room, player, names, 1, 1, hx__sxhx.name,
      "#hx__sxhx-choice", all_names, true)
    if #mark > 0 then
      U.setPrivateMark(player, "$hx__sxhx_names", mark)
    end
  end
})

hx__sxhx:addEffect(fk.CardUsing, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hx__sxhx.name) and table.contains(U.getPrivateMark(player, "$hx__sxhx_names"), data.card.trueName)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {skill_name = hx__sxhx.name, prompt = "#hx__sxhx-invoke::" .. target.id .. ":" .. data.card:toLogString()}) then
      room:doIndicate(player, { target })
      return true
    end
    return false
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark = U.getPrivateMark(player, "$hx__sxhx_names")
    table.removeOne(mark, data.card.trueName)
    if #mark > 0 then
      U.setPrivateMark(player, "$hx__sxhx_names", mark)
    else
      room:setPlayerMark(player, "@[private]$hx__sxhx_names", 0)
    end
    player:drawCards(1, hx__sxhx.name)
    data.toCard = nil
    data:removeAllTargets()
  end,
})


return hx__sxhx
